package com.csii.activiti.server.core.utils;

import com.csii.activiti.server.core.entity.db.ActCommonDataEntity;
import com.csii.activiti.server.core.entity.db.ActModifyCommonDataEntity;

import java.util.LinkedList;
import java.util.List;

public class OracleDBUtils {

    private static final String commentColumnSqlTextTemplate = "COMMENT ON COLUMN \"%s\".\"%s\" IS '%s'";

    private static final String commentTableSqlTextTemplate = "COMMENT ON TABLE \"%s\" IS '%s'";

    private static final String commentColumnRenameSqlTextTemplate = "ALTER TABLE \"%s\" RENAME COLUMN \"%s\" TO \"%s\"";


    public static List<String> createTableSqlText(String tableName, String tableComment, List<ActCommonDataEntity> columnList){

        StringBuilder createTableSqlBuilder = new StringBuilder();
        List<String> sqlTextList = new LinkedList<>();
        //添加主键 注释
        sqlTextList.add(createCommentSqlText(tableName,"ID","主键"));
        //添加表 注释
        sqlTextList.add(createTableSqlText(tableName,tableComment));
        //拼接建表sql
        createTableSqlBuilder.append("CREATE TABLE \"").append(tableName).append("\" (\"ID\" NVARCHAR2(36) NOT NULL,");
        for (ActCommonDataEntity actCommonDataEntity : columnList) {
            if (actCommonDataEntity.getColumnLength() == null){
                createTableSqlBuilder.append(" \"").append(actCommonDataEntity.getColumnName()).append("\" ").append(actCommonDataEntity.getDbType()).append(",");
            }else{
                createTableSqlBuilder.append(" \"").append(actCommonDataEntity.getColumnName()).append("\" ").append(actCommonDataEntity.getDbType()).append("(").append(actCommonDataEntity.getColumnLength()).append(")").append(",");
            }
            //添加注释sql
            sqlTextList.add(createCommentSqlText(tableName,actCommonDataEntity));
        }
        createTableSqlBuilder.append("  PRIMARY KEY (\"ID\"))");
        //把建表sql放入第一个
        sqlTextList.add(0,createTableSqlBuilder.toString());

        return sqlTextList;
    }


    public static String createCommentSqlText(String tableName,ActCommonDataEntity commonDataEntity){
        return createCommentSqlText(tableName,commonDataEntity.getColumnName(),commonDataEntity.getColumnRemark());
    }

    public static String createCommentSqlText(String tableName,String columnName,String columnRemark){
        return String.format(commentColumnSqlTextTemplate,tableName,columnName,columnRemark);
    }

    public static String createTableSqlText(String tableName,String tableComment){
        return String.format(commentTableSqlTextTemplate,tableName,tableComment);
    }

    public static List<String> addColumnSqlText(String tableName, List<ActCommonDataEntity> columnList){
        StringBuilder addColumnSqlBuilder = new StringBuilder();
        addColumnSqlBuilder.append("ALTER TABLE \"").append(tableName).append("\" ");
        List<String> sqlTextList = new LinkedList<>();
        for (ActCommonDataEntity actCommonDataEntity : columnList) {
            if (actCommonDataEntity.getColumnLength() == null){
                addColumnSqlBuilder.append("ADD ").append("(\"").append(actCommonDataEntity.getColumnName()).
                        append("\" ").append(actCommonDataEntity.getDbType()).append(")");
            }else {
                addColumnSqlBuilder.append("ADD ").append("(\"").append(actCommonDataEntity.getColumnName()).
                        append("\" ").append(actCommonDataEntity.getDbType()).append("(").append(actCommonDataEntity.getColumnLength()).append(")").append(")");
            }
            //添加注释sql
            sqlTextList.add(createCommentSqlText(tableName,actCommonDataEntity));
        }
        //把sql放入第一个
        sqlTextList.add(0,addColumnSqlBuilder.toString());
        return sqlTextList;
    }

    public static List<String> modifyColumnSqlText(String tableName, List<ActModifyCommonDataEntity> modifyColumnList){
        StringBuilder modifyColumnSqlBuilder = new StringBuilder();
        modifyColumnSqlBuilder.append("ALTER TABLE \"").append(tableName).append("\" ");
        List<String> sqlTextList = new LinkedList<>();
        boolean dbTypeisModify = false;
        for (ActModifyCommonDataEntity actModifyCommonDataEntity : modifyColumnList) {
            ActCommonDataEntity currentCommonData = actModifyCommonDataEntity.getCurrentCommonData();
            ActCommonDataEntity targetCommonData = actModifyCommonDataEntity.getTargetCommonData();
            if (!currentCommonData.getDbType().equals(targetCommonData.getDbType())){
                dbTypeisModify =true;
                if (targetCommonData.getColumnLength() == null){
                    modifyColumnSqlBuilder.append("MODIFY ").append("(\"").append(currentCommonData.getColumnName()).
                            append("\" ").append(targetCommonData.getDbType()).append(")");
                }else{
                    modifyColumnSqlBuilder.append("MODIFY ").append("(\"").append(currentCommonData.getColumnName()).
                            append("\" ").append(targetCommonData.getDbType()).append("(").append(targetCommonData.getColumnLength()).append(")").append(")");
                }
            }

            //添加名称修改sql 一样无需修改
            if (!currentCommonData.getColumnName().equals(targetCommonData.getColumnName())){
                sqlTextList.add(createColumnRenameSqlText(tableName,currentCommonData.getColumnName(),targetCommonData.getColumnName()));
            }
            //添加注释sql 一样无需修改
            if (!currentCommonData.getColumnRemark().equals(targetCommonData.getColumnRemark())){
                sqlTextList.add(createCommentSqlText(tableName,targetCommonData));
            }
        }

        //把sql放入第一个 ,确定dbType是有调整就添加
        if (dbTypeisModify){
            sqlTextList.add(0,modifyColumnSqlBuilder.toString());
        }
        return sqlTextList;
    }

    public static String createColumnRenameSqlText(String tableName, String currentColumnName, String targetColumnName){
        return String.format(commentColumnRenameSqlTextTemplate,tableName,currentColumnName,targetColumnName);
    }




}
